<?php 
	$project_id = Request::current()->param('project_id', NULL);
	$is_one_project = isset($project_id) ? TRUE : FALSE;
?>
<h3>ALL project</h3>
<?php echo View::factory('project/partials/top_control',array('project' => isset($cur_project) ? $cur_project : NULL ))?>
<div class='tstyle_content'>
	<table class="box-skin" style="width: 85%;">
		<tr class="rowA name_role">
			<td><b>Project name</b></td>
			<td><b>Id</b></td>
			<td><b>Type</b></td>
			<td><b>Priority</b></td>
			<td><b>Progress</b></td>
		</tr>
		<?php foreach ($projects as $project):?>
			<tr class="rowB">
				<td><a href="/project/<?php echo $project['id']?>/setting"><?php echo $project['name']?></a></td>
				<td><?php echo $project['id']?></td>
				<td><?php echo $project['type']?></td>
				<td><?php echo $project['priority']?></td>
				<td>
					<div class="container">
						<div class="progress-bar" style="width:50%;
      						 background:#AF0A0A url('images/a1.gif');
       						 height:1em;">	
  						</div>
  					</div>
  				</td>
			</tr>
		<?php endforeach;?>
	</table>

	<div class='wrap_option' style="width: 14%">
		<div class="option">
		<ul>
			<li><?php echo Form::checkbox('Project name', Helper_MyConst::PROJECT_NAME ,TRUE,array('class'=>'data_option'))?> Project name</li>
			<li><?php echo Form::checkbox('Id', Helper_MyConst::PROJECT_ID,TRUE,array('class'=>'data_option'))?> ID</li>
			<li><?php echo Form::checkbox('Scale', Helper_MyConst::PROJECT_SCALE,FALSE,array('class'=>'data_option'))?> Scale</li>
			<li><?php echo Form::checkbox('Type', Helper_MyConst::PROJECT_TYPE,TRUE,array('class'=>'data_option'))?> Type</li>
			<li><?php echo Form::checkbox('Priotity', Helper_MyConst::PROJECT_PRIORITY,TRUE,array('class'=>'data_option'))?> Priority</li>
			<li><?php echo Form::checkbox('Progress', Helper_MyConst::PROJECT_PROGRESS,TRUE,array('class'=>'data_option'))?> Progress</li>
			<li><?php echo Form::checkbox('Status', Helper_MyConst::PROJECT_STATUS,FALSE,array('class'=>'data_option'))?> Status</li>
			<li><b>Estimates</b></li>
			<li><?php echo Form::checkbox('Estimate start date', Helper_MyConst::PROJECT_ESTIMATE_START_DATE,FALSE,array('class'=>'data_option'))?> Start date</li>
			<li><?php echo Form::checkbox('Deadline', Helper_MyConst::PROJECT_ESTIMATE_END_DATE,FALSE,array('class'=>'data_option'))?> End date</li>
		</ul>
		</div>
		<div class='direction'><a href="#">>></a></div>
	</div>
		
</div>

<script id="project_table" type="text/x-jquery-tmpl">
	<tr class="rowB">
	<td><a href="/project/${id}/setting">${name}</a></td>
	<td>${id}</td>
	<td>${type}</td>
	<td>${priority}</td>
	<td>
		<div class ="container">
			<div class="progress-bar" style="width:${progress}%; background:#AF0A0A height:1em;">	
			</div>
		</div>
	</td>
</tr>
</script>

<script type="text/javascript">
	var projects = $.parseJSON('<?php echo addslashes(json_encode($projects))?>');
	var allOptions = $.parseJSON('<?php echo addslashes(json_encode(Helper_MyConst::$PROJECT_OPTION_2_NAME))?>');
	
	
	$('input.data_option').change(function(){
		//alert($('input.data_option:not(:checked)').length);
		var table = $('div.tstyle_content table');
		var options = [];
		var names = [];
		var progress = false;//Keep track of appear of progress
		//alert($('input.data_option:checked').length);
		$('input.data_option:checked').each(function() {
			if ($(this).val()==<?php echo Helper_MyConst::PROJECT_PROGRESS?>)
				progress = true;
			else
			{
			   names.push($(this).attr('name'));
		       options.push($(this).val());
			}
	     });
	     if (progress)
	     {
	    	 names.push('Progress');
		     options.push('<?php echo Helper_MyConst::PROJECT_PROGRESS?>');
		     progress = true;
	     }
	    renderTable(options, names, projects);
		
	});
	function renderTable1()
	{
		$("div.tstyle_content").find("tr:gt(0)").remove();
	}
	function renderTable(options, names, projects)
	{
		var oLength = options.length;
		var dLength = projects.length;
		var table = $('div.tstyle_content table');
		table.empty();
		var htmlStr = '';
		htmlStr+='<tr class="rowA">';
		for (var i = 0; i<oLength; i++)
			htmlStr+='<td><b>'+names[i]+'</b></td>';
		htmlStr+='</tr>';
		
		for (var i = 0; i<dLength; i++)
		{
			htmlStr+='<tr class="rowB">';
			for(var j = 0; j<oLength; j++)
			{
				if (options[j] == <?php echo Helper_MyConst::PROJECT_NAME?>)
					htmlStr+='<td><a href="/project/'+projects[i].id+'">'+projects[i][allOptions[options[j]]]+'</a></td>';
				else if (options[j] != <?php echo Helper_MyConst::PROJECT_PROGRESS?>)
					htmlStr+='<td>'+projects[i][allOptions[options[j]]]+'</td>';
				else
				{
					htmlStr+='<td><div class="container"><div id="progress-bar" style="width:'+projects[i][allOptions[options[j]]]+'%;';
					htmlStr+='background:#AF0A0A;height:1em;"></div></div></td>';
				}	
			}
			htmlStr+='</tr>';
		}
		table.append(htmlStr);
	}
</script>